import { ElLoading, ElMessage as message, ElMessageBox } from 'element-plus'

export const throttle = (callBack = () => { }, options = { setTime: 2000, showMessage: '请勿重复点击' }, failBack = () => { }) => {
  const { setTime = 2000, showMessage } = options
  let time = false;
  return () => {
    if (!time) {
      time = setTimeout(() => {
        clearTimeout(time)
        time = null
      }, setTime)
      return callBack()
    } else {
      showMessage ? message.warning(showMessage) : ''
      return failBack()
    }
  }
}

export function debounce (fn, time = 1000) {
  // 4.创建一个标记用来存放定时器的返回值
  let timeout;
  return function () {
    // 5.每当用户触发input事件  把前一个 setTimeout 清楚掉
    clearTimeout(timeout);
    // 6.然后又创建一个新的 setTimeout, 这样就能保证输入字符后等待的间隔内 还有字符输入的话，就不会执行 setTimeout里面的内容
    timeout = setTimeout(() => {
      // 7.这里进行防抖的内容
      fn();
    }, time);
  };
}